﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeadExpress.Models.ViewPages;

namespace LeadExpress.Areas.Management.Controllers
{
    public class AccountListController : Controller
    {
        // Create an instance of user manage
        LeadExpress.Repository.UserManagementRepository repository = new LeadExpress.Repository.UserManagementRepository();

        //
        // GET: /Management/Home/
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Index()
        {
            UserManagementModel model = new UserManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", "ClientAdmin", string.Empty, "CreationDate", "desc");
            return View(model);
        }

        [HttpGet]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult GetData(string keywords, int pageSize, int pageIndex, string sortBy, string sortOrder)
        {
            if (keywords.Contains("Keywords"))
                keywords = string.Empty;

            UserManagementModel model = new UserManagementModel();
            model = repository.GetModel("ClientAdmin", keywords, sortBy, sortOrder, pageSize, pageIndex);

            return Json(model, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Locked(string keywords, int pageSize, int pageIndex, string sortBy, string sortOrder, int id)
        {
            if (keywords.Contains("Keywords"))
                keywords = string.Empty;

            // First: Update Locked/Unlock users
            Repository.UserRepository userRepository = new Repository.UserRepository();
            LeadExpress.User user = new LeadExpress.User();
            user = userRepository.GetSingle(id);
            if (user != null)
            {
                user.IsLockedOut = (user.IsLockedOut.Value) ? false : true;
                userRepository.SaveIsLockedOut(user);
            }

            // Second: Get data after lock/unlock
            UserManagementModel model = new UserManagementModel();
            model = repository.GetModel("ClientAdmin", keywords, sortBy, sortOrder, pageSize, pageIndex);

            return Json(model, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult GetUserPerview(int id)
        {
            var model = repository.GetUserModel(id);

            return Json(model, JsonRequestBehavior.AllowGet);
        }

        // GET: Reports Account
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Report()
        {
            UserManagementModel model = new UserManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator", "ClientAdmin", string.Empty, "CreationDate", "desc");
            return View(model);
        }

        public FileResult ExportToExcel()
        {
            LeadExpress.Repository.UserManagementRepository repository = new LeadExpress.Repository.UserManagementRepository();
            FileResult fileReport = repository.ExportToCSVFile();

            return fileReport;
        }
    }
}
